package com.homedo.odin.solution.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.dao.BrandFirmMapper;
import com.homedo.odin.solution.model.dto.BrandFirmBean;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.BrandFirm;
import com.homedo.odin.solution.model.entity.BrandOwner;
import com.homedo.odin.solution.model.request.pc.brand.PCBrandFirmListRequestBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandFirmListResponseBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandFirmPcListResponseBean;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p> 品牌厂商 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class BrandFirmServiceImpl extends BaseService<BrandFirmMapper, BrandFirm> {

    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    /**
     * 根据用户id查找正在审核或审核通过的品牌商信息
     */
    public BrandFirm findFirmByUserIdAndSubmit(Integer userId) {
        return baseMapper.findFirmByUserIdAndSubmit(userId);
    }

    /**
     * 根据用户id和审核状态查找品牌商信息
     */
    public BrandFirm findFirmByUserIdAndStatus(Integer userId, int status) {
        BrandFirm firm = new BrandFirm();
        firm.setUserId(userId);
        firm.setStatus(status);
        return baseMapper.selectOne(firm);
    }

    /**
     * 根据用户id
     */
    public BrandFirm findFirmByUserIdAndStatus(Integer userId) {
        BrandFirm firm = new BrandFirm();
        firm.setUserId(userId);
        return baseMapper.selectOne(firm);
    }

    /**
     * M站_获取品牌商详情
     */
    public BrandFirm getBrandFirmByUserId(Integer userId) {
        return baseMapper.getBrandFirm(userId);
    }

    public PageInfo<BrandFirm> queryBrandFirmByParams(String userId, Integer status,
            PageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        return new PageInfo<BrandFirm>(baseMapper.queryBrandFirmByParams(userId, status));
    }

    public PageInfo<BrandFirmBean> queryBrandFirmByParam(String userId, Integer status, PageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        return new PageInfo<BrandFirmBean>(baseMapper.queryBrandFirmByParam(userId, status));
    }

    public void auditBrandFirm(Integer userId, Integer status, String auditOpinion) {
//        baseMapper.auditBrandFirm(userId, status, auditOpinion);
    }

    /**
     * PC_获取品牌商列表
     */
    public BrandFirmListResponseBean getPcBrandFirmList(PCBrandFirmListRequestBean requestBean) {
        PageHelper.startPage(requestBean.getPageNum(), requestBean.getPageSize());
        PageInfo<BrandFirmPcListResponseBean> pageInfo = new PageInfo<>(baseMapper.getPcBrandFirmList());
        return BeanUtils.transfrom(BrandFirmListResponseBean.class, pageInfo);
    }

    /**
     * 根据brandid获取品牌商信息
     */
    public BrandFirm findFirmByBrandId(int brandId) {
        BrandFirm firm = new BrandFirm();
        firm.setBrandId(brandId);
        return baseMapper.selectOne(firm);
    }

    public List<BrandFirm> getBrandFirmByBrandId(Integer brandId) {
        return baseMapper.getBrandFirmByBrandId(brandId);
    }

    public List<BrandFirmPcListResponseBean> getAllBrandFirm() {
        List<BrandFirmPcListResponseBean> responseList = new ArrayList<>();
        List<BrandFirm> queryAllBrandFirm = baseMapper.getAllBrandFirm();
        if (CollectionUtils.isNotEmpty(queryAllBrandFirm)) {
            queryAllBrandFirm.forEach(brandFirm -> {
                BrandFirmPcListResponseBean resposne = new BrandFirmPcListResponseBean();
                BeanUtils.copyProperties(brandFirm, resposne);
                BrandOwner brandOwner = brandOwnerService.getBrandOwnerById(brandFirm.getBrandId());
                if (brandOwner != null) {
                    resposne.setBrandName(brandOwner.getBrandName());
                    resposne.setBrandIcon(brandOwner.getBrandIcon());
                }
                responseList.add(resposne);
            });
        }
        return responseList;
    }
}
